# -*- coding: utf-8 -*-
"""
Created on Tue Oct 09 19:05:11 2012

@author: Tomek
"""

import namespace
import sys
#sys.argv[1]==compiled namespace name
ns=None

if len(sys.argv)!=2:
    #only for debug
    if namespace._DEBUG_:
        ns=namespace.loadNamespace("_ns_")
else:
    fullpath=""
    for p in range(1,len(sys.argv)):
        fullpath=fullpath+" "+sys.argv[p]
    ns=namespace.loadNamespace(fullpath)
class Template:
    def __init__(self,templates,name):
        self.templates=templates
        self.name=name
    def merge(self,a):
        if self.templates!=a.templates:
            return False
        else:
            if self.name=="":
                self.name=a.name
            return True
        return False
    def equals(self,a):
        return self.templates==a.templates
    def generateName(self,name):
        if self.name=="":
            templateTable=self.templates.split(",")
            self.name=name
            for t in templateTable:
                if t!="":
                    self.name=self.name+"_"+t
def tableContainsMerge(tbl,x):
    for y in tbl:
        if y.name==x.name:
            y.merge(x)
            return True
    return False
class ScanType:
    def __init__(self,name,template):
        self.name=name
        self.template=[template]
    def merge(self,a):
        if a.name==self.name:
            for x in a.template:
                found=False
                for y in self.template:
                    if y.equals(x):
                        found=True
                        y.merge(x)
                if found==False:
                    self.template.append(x)
    def generateName(self):
        for t in self.template:
            t.generateName(self.name)
scanTypes=[]
for xx in ns.getObjects("Typedef"):
    x=xx["type"]
    withTemplates=False
    for t in x["templates"]:
        if t["isTemplate"]:
            withTemplates=True
    if x["isBuiltin"]==False and x["isTemplate"]==False and withTemplates==False:
        splitNamespace=x["name"].split("::")
        onlyName=splitNamespace[len(splitNamespace)-1]
        tstr=""
        for t in x["templates"]:
            if tstr!="":
                tstr=tstr+","
            tstr=tstr+t["fullname"]
        r=ScanType(onlyName,Template(tstr,xx["name"]))
        if tableContainsMerge(scanTypes,r)==False:
            scanTypes.append(r)
for x in ns.getObjects("Type"):
    withTemplates=False
    for t in x["templates"]:
        if t["isTemplate"]:
            withTemplates=True
    if x["isBuiltin"]==False and x["isTemplate"]==False and withTemplates==False:
        splitNamespace=x["name"].split("::")
        onlyName=splitNamespace[len(splitNamespace)-1]
        tstr=""
        for t in x["templates"]:
            if tstr!="":
                tstr=tstr+","
            tstr=tstr+t["fullname"]
        r=ScanType(onlyName,Template(tstr,""))
        if tableContainsMerge(scanTypes,r)==False:
            scanTypes.append(r)
for x in scanTypes:
    x.generateName()
    tstr=""
    
    for t in x.template:
        if tstr=="":
            tstr="|"
        else:
            tstr=tstr+";"
        tstr=tstr+t.templates+"="+t.name
    print x.name+tstr
print "CLOSE_STREAM_PYTHON"